package STO_39;

import java.util.HashMap;
import java.util.Map;

public class Solution {
	public int majorityElement(int[] nums) {
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();

		for (int i = 0; i < nums.length; i++) {
			if (map.containsKey(nums[i])) {
				int value = map.get(nums[i]);
				map.put(nums[i], value + 1);
			} else {
				map.put(nums[i], 1);
			}
		}
		int ret = -1;
		for (int i = 0; i < nums.length; i++) {
			if (map.get(nums[i]) > nums.length / 2) {
				ret = nums[i];
			}
		}
		return ret;

	}

	public int majorityElement_vote(int[] nums) {
		int x = 0, vote = 0;
		for (int num : nums) {
			if (vote == 0) {
				x = num;
			}

			vote += num == x ? 1 : -1;

		}
		return x;

	}
}
